package com.github.caijh.mall.system.controller;

import javax.validation.Valid;

import com.github.caijh.framework.core.exception.BizException;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.MessageConstants;
import com.github.caijh.mall.system.model.SystemAdmin;
import com.github.caijh.mall.system.request.SystemAdminAddReqBody;
import com.github.caijh.mall.system.request.SystemAdminReqBody;
import com.github.caijh.mall.system.request.SystemAdminUpdateReqBody;
import com.github.caijh.mall.system.response.SystemAdminRespBody;
import com.github.caijh.mall.system.service.SystemAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/admin/system/admin")
@Api(tags = "后台用户服务")

public class SystemAdminController extends BaseController {

    @Autowired
    private SystemAdminService systemAdminService;

    /**
     * 分页显示后台管理员表
     *
     * @param reqBody 搜索条件
     * @return Page
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public CommonResult<CommonPage<SystemAdminRespBody>> list(@Validated SystemAdminReqBody reqBody) {
        CommonPage<SystemAdminRespBody> systemAdminCommonPage = CommonPage.restPage(systemAdminService.getList(reqBody));
        return CommonResult.success(systemAdminCommonPage);
    }

    /**
     * 新增后台管理员表
     *
     * @param systemAdminAddReqBody 新增参数
     * @return SystemAdminRespBody
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public SystemAdminRespBody save(@RequestBody @Validated SystemAdminAddReqBody systemAdminAddReqBody) {
        SystemAdmin systemAdmin = new SystemAdmin();
        BeanUtils.copyProperties(systemAdminAddReqBody, systemAdmin);
        return systemAdminService.add(systemAdminAddReqBody);
    }

    /**
     * 删除后台管理员表
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-13
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    public void delete(@RequestParam(value = "id") Integer id) {
        systemAdminService.removeById(id);
    }

    /**
     * 修改后台管理员表
     *
     * @param reqBody 修改参数
     * @author Mr.Zhang
     * @since 2020-04-13
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public void update(@RequestBody @Validated SystemAdminUpdateReqBody reqBody) {
        systemAdminService.update(reqBody);
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    public SystemAdmin info(@RequestParam(value = "id") Integer id) {
        return systemAdminService.getById(id);
    }

    /**
     * 修改后台管理员状态
     *
     * @param id     admin user id
     * @param status true to enable, false to disable
     */
    @ApiOperation(value = "修改后台管理员状态")
    @GetMapping(value = "/updateStatus")
    public void updateStatus(@RequestParam(value = "id") @Valid Integer id, @RequestParam(value = "status") @Valid Boolean status) {
        boolean success = systemAdminService.updateStatus(id, status);
        if (!success) {
            throw BizException.of(MessageConstants.OPERATE_UPDATE_FAIL);
        }
    }

}



